require "Common/define"
require "Logic/Config"
require "FairyGUI"

UISevenday = {};
HelperFunc.SetUIEnv(UISevenday)
local this = UISevenday;

this.LuaBehaviour = nil;
this.transform = nil;
this.gameObject = nil;
this.hasInit = false;
this.RewardBtn = nil;
this.HasRewardBtn = nil;
this.NowDay = 1;
this.DayBtnTab = {}
this.SevenDayItem = {}
this.Backbtn = nil;
this.ImageName = nil;
this.Des = nil;
this.RoleLihui = nil;
this.drawWrapper = nil;
this.SsrHero = nil;

function UISevenday.Init(callback)
    this.callBack = callback;
    local name, packageName = getUIName(PanelNames.UISevenday);
    panelMgr:ShowUI(name, packageName, this.OnCreate, Config.useLocalUI);
end

function UISevenday.OnCreate(obj)
    if not hasInit then
        hasInit = true;
        --RewardBtn=obj:GetChild("RewardBtn")
        Backbtn = obj:GetChild("bgClose")
        ImageName = obj:GetChild("lihui");
        --SsrHero=obj:GetChild("ssrHero")
        SsrHeroTiTle = obj:GetChild("text1")
        this.DayGroup = obj:GetChild("DayGroup")
        this.DayGroupTransition = this.DayGroup:GetTransition("DayGroup")
        this.heroNameController = obj:GetController("heroName")
        --obj:GetChild("Title").text = getLanguage("ActivityTitile_SevenDay");
        --drawWrapper = GoWrapper.New()
        for i = 1, 7 do
            local tempDay = this.DayGroup:GetChild("Day" .. i);
            table.insert(DayBtnTab, tempDay);
        end
        this.addClick();
    end
    this.DayGroupTransition:Play();
    this.initSevenday();
    if NowDay == 1 then
        this.onClickDay(NowDay);
    end
    logicMgr.HeroManager.setGetNewHeroShow(true);
end

function UISevenday.OnEnable()
    this.LocalListenEvent();
end

function UISevenday.OnDisable()
    --UpdateBeat:Remove(this.update, slef);
    --CloseEvent(Config.EventType.Fresh_Shop,this.refreshUI);
end

function UISevenday.LocalListenEvent()
    --ListenEvent(Config.EventType.Fresh_Shop,this.refreshUI);
end

function UISevenday.OnDestroy()
    this.removeClick();
    this.DayBtnTab = {};
end

function UISevenday.addClick()
    for i = 1, 7 do
        -- LuaBehaviour:AddClick(DayBtnTab[i],function() this.onClickDay(i); end);
        DayBtnTab[i].name = i;
        DayBtnTab[i].onClick:Add(this.onClickDayIndex);
    end
    -- LuaBehaviour:AddClick(RewardBtn,this.onGetReward);
    -- LuaBehaviour:AddClick(Backbtn,this.onClickBack);
    --RewardBtn.onClick:Add(this.onGetReward);
    Backbtn.onClick:Add(this.onClickBack);
    ListenEvent(Config.EventType.Fresh_SevenDay, this.initSevenday);
end

function UISevenday.removeClick()
    for i = 1, 7 do
        -- LuaBehaviour:RemoveClick(DayBtnTab[i]);
        DayBtnTab[i].onClick:Remove(this.onClickDayIndex);
    end
    -- LuaBehaviour:RemoveClick(RewardBtn);
    -- LuaBehaviour:RemoveClick(Backbtn);
    --RewardBtn.onClick:Remove(this.onGetReward);
    Backbtn.onClick:Remove(this.onClickBack);
    CloseEvent(Config.EventType.Fresh_SevenDay, this.initSevenday);
end

function UISevenday.onClickDayIndex(evt)
    this.onClickDay(tonumber(evt.sender.name))
    this.onGetReward();
end

function UISevenday.onClickDay(index)
    NowDay = index;
end

function UISevenday.onGetReward()
    local tempinfo = logicMgr.ActiveManager.sevenDayInfotab[NowDay];
    if (tempinfo ~= nil) then
        if (tempinfo.draw == 0) then
            ActivityHandler.sendReqSevenDayDrawMessage(NowDay);
            --RewardBtn.visible = false;
            --local tempconfig = G.dataTable["t_sevenday"][tostring(NowDay)]
            --local Rewardlist = {}
            --local TempReward = decodeJsonStrToTabel(tempconfig.f_Reward);
            --for i = 1, #TempReward do
            --    local tempItemID = TempReward[i]
            --    local tempReward = logicMgr.ItemManager.getItemConstDatabyModelId(tempItemID[1]);
            --    tempReward["num"] = tempItemID[2]
            --    table.insert(Rewardlist, tempReward);
            --end
            --showUI(PanelNames.UICommonItemGet, nil, nil, 2, Rewardlist);
        end
    end
end

function UISevenday.showReward(day, rewardlist)
    --local Rewardlist = {};
    --local tempconfig = G.dataTable["t_sevenday"][tostring(NowDay)]
    --local TempReward = decodeJsonStrToTabel(tempconfig.f_Reward);
    --for i = 1, #TempReward do
    --    local itemData = logicMgr.CostManager.getItemOrEquipDataByIdAndNum(TempReward[i][1], TempReward[i][2]);
    --    if itemData ~= nil and itemData.itemType ~= EnumConst.ItemTypeEnum.HeroEntity then
    --        table.insert(Rewardlist, itemData);
    --    end
    --end
    if #rewardlist > 0 then
        showUI(PanelNames.UICommonItemGet, nil, nil, 3, rewardlist);
    end
    logicMgr.ActiveManager.sevenDayInfotab[NowDay].draw = 1;
    local drawNum = 0;
    for k, v in pairs(logicMgr.ActiveManager.sevenDayInfotab) do
        drawNum = drawNum + v.draw;
    end
    if drawNum >= 7 then
        logicMgr.ActiveManager.removeOneActive(10001);
        this.onClickBack();
    end
end

function UISevenday.initSevenday()

    local SevendayDb = G.dataTable["t_sevenday"];

    local hasTurn = false;
    local lastDay = 1;
    for k, v in pairsByKeys(SevendayDb) do
        local key = tonumber(k);
        local tempAcitveBtn = DayBtnTab[key];
        --local Reward = decodeJsonStrToTabel(v.f_Reward);
        -- local tempSevenItem=SevenDayItem[key]
        --local itemBase = logicMgr.ItemManager.getItemConstDatabyModelId(Reward[1][1])
        --if (itemBase == nil) then
        --    itemBase = dataTable.getDataTableByStringId("t_equip",Reward[1][1]);
        --    if (itemBase ~= nil and k ~= "7") then
        --        local iconS = tempAcitveBtn:GetChild("Icon")
        --        iconS.url = "ui://ItemAtlas/" .. itemBase.f_EquipSuitlcon;
        --    end
        --else
        --    if (itemBase ~= nil and k ~= "7") then
        --        local iconS = tempAcitveBtn:GetChild("Icon")
        --        iconS.url = "ui://ItemAtlas/" .. itemBase["icon"];
        --    end
        --end
        local tempinfo = logicMgr.ActiveManager.sevenDayInfotab[key];
        local notCanClick = nil;
        if (tempinfo ~= nil) then
            if lastDay < key then
                lastDay = key;
            end
            tempAcitveBtn:GetController("flag").selectedIndex = tempinfo.draw == 1 and 1 or 0;
            tempAcitveBtn:GetController("redDot").selectedIndex = tempinfo.draw == 0 and 1 or 0;
            notCanClick = tempinfo.draw == 0;
            if (tempinfo.draw == 0 and hasTurn == false) then
                hasTurn = true;
                NowDay = tonumber(k);
                this.onClickDay(NowDay);
            end
        else
            tempAcitveBtn:GetController("flag").selectedIndex = 0;
            tempAcitveBtn:GetController("redDot").selectedIndex = 0;
        end
        local showBigNum = nil;
        --if key ~= 2 then
        --    showBigNum = {isShowBigNum=true}
        --end
        logicMgr.CostManager.RefreshItemList(tempAcitveBtn:GetChild("list"), decodeJsonStrToTabel(v.f_Reward), nil, showBigNum);
        logicMgr.CostManager.RefreshItemList(tempAcitveBtn:GetChild("list2"), decodeJsonStrToTabel(v.f_Reward), nil, showBigNum);
        --local dayNumtext = string.format(getLanguage(v.f_Describe), k);
        --if (k ~= "7") then
        tempAcitveBtn:GetChild("DayText").text = string.format(getLanguage("Sevenday_1"), k);
        --tempAcitveBtn:GetChild("DayNum").text = dayNumtext;
        --end
    end
    if lastDay > 2 then
        this.heroNameController.selectedIndex = 1;
    else
        this.heroNameController.selectedIndex = 0;
    end
    --ImageName.url = "ui://UISevenday/" .. SevendayDb[tostring(lastDay)].f_AwardPicture;
    SsrHeroTiTle.text = getLanguage(SevendayDb[tostring(lastDay)].f_AwardTitle);
end

function UISevenday.update()
    --看板娘
    -- local curTime = os.time(); --timeToTimeLeft(timeOffset);
end

function UISevenday.setData(itemobj, datasource)
    -- local item = CtrlManager.GetCtrl(PanelNames.Item);
    -- if item ~= nil then
    --     item.initData(itemobj,datasource,LuaBehaviour);
    -- end
end

--返回
function UISevenday.onClickBack()
    logicMgr.HeroManager.setGetNewHeroShow();
    closeUI(PanelNames.UISevenday);
    if this.callBack ~= nil then
        this.callBack();
    end
    logicMgr.ActiveManager.showPopUpTips();
end

return UISevenday;